﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LotteryVote.Utility
{
    public class EnumHelper
    {
        public static T GetEnum<T>(string value) where T : struct
        {
            T result;
            if (!Enum.TryParse<T>(value, out result))
                result = default(T);
            return result;
        }

        public static IList<T> ToList<T>() where T : struct
        {
            Type enumType = typeof(T);
            if (!enumType.IsEnum)
                return null;
            List<T> content = new List<T>();
            foreach (var name in Enum.GetNames(enumType))
            {
                content.Add(GetEnum<T>(name));
            }
            return content;
        }
        public static IList<EnumDescriptionAttribute> GetDescription<T>() where T : struct
        {
            Type enumType = typeof(T);
            if (!enumType.IsEnum)
                throw new ArgumentException(enumType.FullName + " 不是枚举类型!");
            List<EnumDescriptionAttribute> descList = new List<EnumDescriptionAttribute>();
            foreach (var name in Enum.GetNames(enumType))
            {
                descList.Add(EnumDescriptionAttribute.GetEnumDesc(GetEnum<T>(name)));
            }
            return descList;
        }
        public static EnumDescriptionAttribute GetEnumDescript(Enum enumValue)
        {
            return EnumDescriptionAttribute.GetEnumDesc(enumValue);
        }
    }
}
